﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SkypeStudio.Controls.Services;
using SkypeStudio.Controls.Views;
using Rhino.Mocks.Interfaces;
using Rhino.Mocks;
using SkypeStudio.Controls.ViewModels;
using SkypeStudio.Controls.Model;

namespace SkypeStudio.Controls.Test.ViewModel
{
    public class MockChatListView : IChatListView
    {

        public object DataContext { get; set; }
    }
    [TestClass]
    public class ChatListVieModelTest : BaseTest
    {
        [TestMethod]
        public void ListeningSkypeEvent()
        {
            ISkypeApplicationService skypeService = _Mock.StrictMock<ISkypeApplicationService>();
            IChatListView view = new MockChatListView();
            IEventRaiser skypeProcessAttachStatusChanged = null;
            IEventRaiser skypeMessageReceived = null;

            List<SkypeStudio.Controls.Model.Chat> chats = new List<Chat>();
            using (_Mock.Record())
            {
                skypeProcessAttachStatusChanged = Expect.Call(delegate { skypeService.SkypeProcessAttachStatusChanged += null; }).IgnoreArguments().GetEventRaiser();
                skypeMessageReceived = Expect.Call(delegate { skypeService.MessageReceived += null; }).IgnoreArguments().GetEventRaiser();
                Expect.Call(skypeService.GetChatList()).Return(chats);
            }

            using (_Mock.Playback())
            {
                ChatListViewModel target = new ChatListViewModel(view, skypeService);
                List<string> propertyChangeLsit = new List<string>();
                target.PropertyChanged += (s, e) => { propertyChangeLsit.Add(e.PropertyName); };
                skypeProcessAttachStatusChanged.Raise(SkypeProcessAttachStatus.apiAttachSuccess);

                //Assert.AreEqual(3, propertyChangeLsit.Count);
                Assert.IsTrue(propertyChangeLsit.Any(o => o == "Chats"));
                //Assert.IsTrue(propertyChangeLsit.Any(o => o == "IsSkypeProcessAttachedReady"));
                //Assert.IsTrue(propertyChangeLsit.Any(o => o == "IsNoySkypeProcessAttachedReady"));

                Assert.AreEqual(0, target.Chats.Count);

                skypeMessageReceived.Raise(new ChatMessage() { ChatName = "usernittestchat", Message = "A", User = new Contact() {  Handle = "d"} });
                Assert.IsNotNull(target.Chats);
                Assert.AreEqual(1, target.Chats.Count);
                Assert.IsTrue(target.Chats.Any(o => o.ChatName == "usernittestchat"));
                Assert.IsTrue(target.Chats.Any(o => o.ChatMessages.Any(x=>x.Message == "A")));

                skypeMessageReceived.Raise(new ChatMessage() { ChatName = "usernittestchat", Message = "b", User = new Contact() { Handle = "d" } });
                Assert.AreEqual(1, target.Chats.Count);
                Assert.IsTrue(target.Chats.Any(o => o.ChatName == "usernittestchat"));
                Assert.IsTrue(target.Chats.Any(o => o.ChatMessages.Any(x => x.Message == "b")));
            }
        }
    }
}
